﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for MyMembershipProvider
/// </summary>
public class MyMembershipProvider : MembershipProvider
{
    private bool _requiresQuestionAndAnswer = true;
    public MyMembershipProvider()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        throw new NotImplementedException();
    }

    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
    {
        throw new NotImplementedException();
    }

    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        MembershipUser newUser = null;
        try
        {
            newUser = new MembershipUser(
            "MyMembershipProvider",
            username, null, email, String.Empty,
             String.Empty, true, false, DateTime.Now,
             DateTime.Now, DateTime.Now, DateTime.Now,
             DateTime.Now
            );
            status = MembershipCreateStatus.Success;



        }
        catch (Exception ex)
        {
            status = MembershipCreateStatus.ProviderError;
            newUser = null;
            throw ex;
        }
        finally
        {
            DataClassesDataContext db = new DataClassesDataContext();
            Klant klant = new Klant();
            klant.EMAILADRES = email;
            klant.WACHTWOORD = password;
            klant.GEBRUIKERSNAAM = username;
            klant.DATUM = DateTime.Now;
            db.Klants.InsertOnSubmit(klant);
            db.SubmitChanges();
        }
        return newUser;
    }

    public override bool DeleteUser(string username, bool deleteAllRelatedData)
    {
        throw new NotImplementedException();
    }

    public override bool EnablePasswordReset
    {
        get { throw new NotImplementedException(); }
    }

    public override bool EnablePasswordRetrieval
    {
        get { throw new NotImplementedException(); }
    }

    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }

    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }

    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotImplementedException();
    }

    public override int GetNumberOfUsersOnline()
    {
        throw new NotImplementedException();
    }

    public override string GetPassword(string username, string answer)
    {
        throw new NotImplementedException();
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        username = username.ToLower();

        DataClassesDataContext db = new DataClassesDataContext();
        var gebruiker =
            from k in db.Klants
            where k.EMAILADRES == username
            select k;

        var gebruikerinfo = gebruiker.ToArray();
        if ((gebruiker == null) || (gebruikerinfo.Length < 1)) return null;

        MembershipUser retVal = new MembershipUser("MyMembershipProvider", gebruikerinfo[0].GEBRUIKERSNAAM, gebruikerinfo[0].KLANT_ID,
        gebruikerinfo[0].EMAILADRES, String.Empty, "Comments!", true, false, DateTime.MinValue,
        DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);

        return retVal;
    } 

    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        throw new NotImplementedException();
    }

    public override string GetUserNameByEmail(string email)
    {
        throw new NotImplementedException();
    }

    public override int MaxInvalidPasswordAttempts
    {
        get { throw new NotImplementedException(); }
    }

    public override int MinRequiredNonAlphanumericCharacters
    {
        get { throw new NotImplementedException(); }
    }

    public override int MinRequiredPasswordLength
    {
        get { throw new NotImplementedException(); }
    }

    public override int PasswordAttemptWindow
    {
        get { throw new NotImplementedException(); }
    }

    public override MembershipPasswordFormat PasswordFormat
    {
        get { throw new NotImplementedException(); }
    }

    public override string PasswordStrengthRegularExpression
    {
        get { throw new NotImplementedException(); }
    }

    public override bool RequiresQuestionAndAnswer
    {
        get { return _requiresQuestionAndAnswer; }
    }

    public override bool RequiresUniqueEmail
    {
        get { throw new NotImplementedException(); }
    }

    public override string ResetPassword(string username, string answer)
    {
        throw new NotImplementedException();
    }

    public override bool UnlockUser(string userName)
    {
        throw new NotImplementedException();
    }

    public override void UpdateUser(MembershipUser user)
    {
        throw new NotImplementedException();
    }

    public override bool ValidateUser(string username, string password)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var login = from k in db.Klants
                    where k.EMAILADRES == username && k.WACHTWOORD == password
                    select k;

        if (login.Count() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
